package work.skc.customer_task_record;


import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ColumnWidth(value = 20)
public class CustomerExcelObject {

    @ExcelProperty("三方卡号")
    private String thirdCode;
    @ExcelProperty("用户ID")
    private String customerId;
    @ExcelProperty("手机号")
    private String mobile;
    @ExcelProperty("OpenId")
    private String miniOpenId;
    @ExcelProperty("UnionId")
    private String miniUnionId;
    @ExcelProperty("用户姓名")
    private String realName;
    @ExcelProperty("用户昵称")
    private String nickName;
    @ExcelProperty("用户头像")
    private String avatarUrl;
    @ExcelProperty("是否找到")
    private boolean hit;

    @ExcelProperty("是否重复")
    private String isMulti;

    @ExcelProperty("错误内容")
    private String errMsg;

    public static CustomerExcelObject form(String thiId) {
        return builder()
                .thirdCode(thiId)
                .hit(false)
                .isMulti("否")
                .build();
    }

    public static CustomerExcelObject form(JSONObject jsonObject, boolean isMulti) {
        if (!jsonObject.containsKey("basicInfo") || !jsonObject.containsKey("miniProgramUserInfoList")) {
            return null;
        }
        JSONObject basicInfo = jsonObject.getJSONObject("basicInfo");
        JSONObject miniProgramUserInfoList = jsonObject.getJSONArray("miniProgramUserInfoList").getJSONObject(0);

        return builder()
                .thirdCode(basicInfo.getStr("thirdCode"))
                .customerId(jsonObject.getStr("id"))
                .mobile("待填充")
                .miniOpenId(miniProgramUserInfoList.getStr("openId"))
                .miniUnionId(miniProgramUserInfoList.getStr("unionId"))
                .realName(basicInfo.getStr("realName"))
                .nickName(basicInfo.getStr("nickName"))
                .avatarUrl(basicInfo.getStr("avatarUrl"))
                .hit(true)
                .isMulti(isMulti?"是":"否")
                .build();
    }

    public String toItem() {
        //2023-09-25T13:33:17.955Z
        Date date = new Date();
        String dateFmt = DateUtil.format(date,"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        return String.format(Setting.inserItemSql, UUID.randomUUID().toString(), customerId, dateFmt,customerId,realName,nickName,avatarUrl,mobile,thirdCode,miniOpenId,miniUnionId,dateFmt,dateFmt);
//        return null;
    }
}
